package main

import (
	"arthm_manip"
	"boolean_manip"
	"errchk_manip"
	"flag"
	"fmt"
	"manip_exec"
	"os/exec"
	"strconv"
	"varnam_manip"
)

const (
	PACKAGE_NAME = iota
	RANDSEQ
	ITERATIONS
)

var Executor = &manip_exec.Manip_Exec{make([]manip_exec.Manipulator, 0), make([]int, 10)}

func main() {

	flag.Parse()
	args := flag.Args()
	args, iters := catchArgs(args)

	Executor.AddManipulator(arthm_manip.ArthmManipulator{})
	Executor.AddManipulator(boolean_manip.BoolManipulator{})
	Executor.AddManipulator(errchk_manip.ErrManipulator{})
	Executor.AddManipulator(varnam_manip.VarManipulator{})

	checkBuild()

	exec_args := make([]string, 0)
	exec_args = append(exec_args, args[PACKAGE_NAME], args[RANDSEQ]) //Input file, randseq

	Executor.ManipulateAll(exec_args, iters)

}

func catchArgs(args []string) ([]string, int) {

	var iters int

	if len(args) < 2 {
		args = append(args, strconv.FormatInt(int64(1), 10))
	}

	if len(args) < 3 {
		iters = 20
		args = append(args, strconv.FormatInt(int64(20), 10))
	} else if len(args) == 3 {
		i, err := strconv.ParseInt(args[2], 0, 0)
		if err != nil {
			panic("Conv failed")
		}
		iters = int(i)
	}

	return args, iters
}

func checkBuild() {

	cmd := exec.Command("go", "install", "manip_exec")
	output, err1 := cmd.Output()

	if err1 != nil {
		fmt.Printf("%s\n", output)
		panic("Build failed")
	}

}

// fset := token.NewFileSet()
// f, err := parser.ParseFile(fset, cmd_args[INPUT_FILE], nil, 0)
